home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / POV-Ray 3.0.2 / src / SOURCE / LIBPNG / PNGMEM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-16  |  8.0 KB  |  315 lines  |  [TEXT/CWIE]

  1.  
  2. /* pngmem.c - stub functions for memory allocation
  3.  
  4.    libpng 1.0 beta 4 - version 0.90
  5.    For conditions of distribution and use, see copyright notice in png.h
  6.    Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  7.    January 10, 1997
  8.  
  9.    This file provides a location for all memory allocation.  Users which
  10.    need special memory handling are expected to modify the code in this file
  11.    to meet their needs.  See the instructions at each function. */
  12.  
  13. #define PNG_INTERNAL
  14. #include "png.h"
  15.  
  16. /* Borland DOS special memory handler */
  17. #if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
  18. /* if you change this, be sure to change the one in png.h also */
  19.  
  20. /* Allocate memory for a png_struct.  The malloc and memset can be replaced
  21.    by a single call to calloc() if this is thought to improve performance. */
  22. png_voidp
  23. png_create_struct(int type)
  24. {
  25.    png_size_t size;
  26.    png_voidp struct_ptr;
  27.  
  28.    if (type == PNG_STRUCT_INFO)
  29.      size = sizeof(png_info);
  30.    else if (type == PNG_STRUCT_PNG)
  31.      size = sizeof(png_struct);
  32.    else
  33.      return (png_voidp)NULL;
  34.  
  35.    if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
  36.    {
  37.       png_memset(struct_ptr, 0, size);
  38.    }
  39.  
  40.    return (struct_ptr);
  41. }
  42.  
  43.  
  44. /* Free memory allocated by a png_create_struct() call */
  45. void
  46. png_destroy_struct(png_voidp struct_ptr)
  47. {
  48.    if (struct_ptr)
  49.       farfree (struct_ptr);
  50. }
  51.  
  52. /* Allocate memory.  For reasonable files, size should never exceed
  53.    64K.  However, zlib may allocate more then 64K if you don't tell
  54.    it not to.  See zconf.h and png.h for more information. zlib does
  55.    need to allocate exactly 64K, so whatever you call here must
  56.    have the ability to do that. */
  57.  
  58. /* Borland seems to have a problem in DOS mode for exactly 64K.
  59.    It gives you a segment with an offset of 8 (perhaps to store it's
  60.    memory stuff).  zlib doesn't like this at all, so we have to
  61.    detect and deal with it.  This code should not be needed in
  62.    Windows or OS/2 modes, and only in 16 bit mode.  This code has
  63.    been updated by Alexander Lehmann for version 0.89 to waste less
  64.    memory.
  65. */
  66.  
  67. png_voidp
  68. png_malloc(png_structp png_ptr, png_uint_32 size)
  69. {
  70.    png_voidp ret;
  71.    if (!png_ptr || !size)
  72.       return ((voidp)NULL);
  73.  
  74. #ifdef PNG_MAX_MALLOC_64K
  75.    if (size > (png_uint_32)65536L)
  76.       png_error(png_ptr, "Cannot Allocate > 64K");
  77. #endif
  78.  
  79.    if (size == (png_uint_32)(65536L))
  80.    {
  81.       if (!png_ptr->offset_table)
  82.       {
  83.          /* try to see if we need to do any of this fancy stuff */
  84.          ret = farmalloc(size);
  85.          if (!ret || ((long)ret & 0xffff))
  86.          {
  87.             int num_blocks;
  88.             png_uint_32 total_size;
  89.             png_bytep table;
  90.             int i;
  91.             png_byte huge * hptr;
  92.  
  93.             if (ret)
  94.                farfree(ret);
  95.             ret = NULL;
  96.  
  97.             num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
  98.             if (num_blocks < 1)
  99.                num_blocks = 1;
  100.             if (png_ptr->zlib_mem_level >= 7)
  101.                num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
  102.             else
  103.                num_blocks++;
  104.  
  105.             total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks+16;
  106.  
  107.             table = farmalloc(total_size);
  108.  
  109.             if (!table)
  110.             {
  111.                png_error(png_ptr, "Out of Memory");
  112.             }
  113.  
  114.             if ((long)table & 0xfff0)
  115.             {
  116.                png_error(png_ptr, "Farmalloc didn't return normalized pointer");
  117.             }
  118.  
  119.             png_ptr->offset_table = table;
  120.             png_ptr->offset_table_ptr = farmalloc(
  121.                num_blocks * sizeof (png_bytep));
  122.  
  123.             if (!png_ptr->offset_table_ptr)
  124.             {
  125.                png_error(png_ptr, "Out of memory");
  126.             }
  127.  
  128.             hptr = (png_byte huge *)table;
  129.             if ((long)hptr & 0xf)
  130.             {
  131.                hptr = (png_byte huge *)((long)(hptr) & 0xfffffff0L);
  132.                hptr += 16L;
  133.             }
  134.             for (i = 0; i < num_blocks; i++)
  135.             {
  136.                png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
  137.                hptr += 65536L;
  138.             }
  139.  
  140.             png_ptr->offset_table_number = num_blocks;
  141.             png_ptr->offset_table_count = 0;
  142.             png_ptr->offset_table_count_free = 0;
  143.          }
  144.       }
  145.  
  146.       if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
  147.          png_error(png_ptr, "Out of Memory");
  148.  
  149.       ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
  150.    }
  151.    else
  152.       ret = farmalloc(size);
  153.  
  154.    if (ret == NULL)
  155.    {
  156.       png_error(png_ptr, "Out of Memory");
  157.    }
  158.  
  159.    return ret;
  160. }
  161.  
  162. /* free a pointer allocated by png_malloc().  In the default
  163.    configuration, png_ptr is not used, but is passed in case it
  164.    is needed.  If ptr is NULL, return without taking any action. */
  165. void
  166. png_free(png_structp png_ptr, png_voidp ptr)
  167. {
  168.    if (!png_ptr)
  169.       return;
  170.  
  171.    if (ptr != NULL)
  172.    {
  173.       if (png_ptr->offset_table)
  174.       {
  175.          int i;
  176.  
  177.          for (i = 0; i < png_ptr->offset_table_count; i++)
  178.          {
  179.             if (ptr == png_ptr->offset_table_ptr[i])
  180.             {
  181.                ptr = 0;
  182.                png_ptr->offset_table_count_free++;
  183.                break;
  184.             }
  185.          }
  186.          if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
  187.          {
  188.             farfree(png_ptr->offset_table);
  189.             farfree(png_ptr->offset_table_ptr);
  190.             png_ptr->offset_table = 0;
  191.             png_ptr->offset_table_ptr = 0;
  192.          }
  193.       }
  194.  
  195.       if (ptr)
  196.          farfree(ptr);
  197.    }
  198. }
  199.  
  200. #else /* Not the Borland DOS special memory handler */
  201.  
  202. /* Allocate memory for a png_struct or a png_info.  The malloc and
  203.    memset can be replaced by a single call to calloc() if this is thought
  204.    to improve performance noticably.*/
  205. png_voidp
  206. png_create_struct(int type)
  207. {
  208.    size_t size;
  209.    png_voidp struct_ptr;
  210.  
  211.    if (type == PNG_STRUCT_INFO)
  212.      size = sizeof(png_info);
  213.    else if (type == PNG_STRUCT_PNG)
  214.      size = sizeof(png_struct);
  215.    else
  216.      return (png_voidp)NULL;
  217.  
  218. #if defined(__TURBOC__) && !defined(__FLAT__)
  219.    if ((struct_ptr = (png_voidp)farmalloc(size)) != NULL)
  220. #else
  221. # if defined(_MSC_VER) && defined(MAXSEG_64K)
  222.    if ((struct_ptr = (png_voidp)halloc(size,1)) != NULL)
  223. # else
  224.    if ((struct_ptr = (png_voidp)malloc(size)) != NULL)
  225. # endif
  226. #endif
  227.    {
  228.       png_memset(struct_ptr, 0, size);
  229.    }
  230.  
  231.    return (struct_ptr);
  232. }
  233.  
  234.  
  235. /* Free memory allocated by a png_create_struct() call */
  236. void
  237. png_destroy_struct(png_voidp struct_ptr)
  238. {
  239.    if (struct_ptr)
  240. #if defined(__TURBOC__) && !defined(__FLAT__)
  241.       farfree(struct_ptr);
  242. #else
  243. # if defined(_MSC_VER) && defined(MAXSEG_64K)
  244.       hfree(struct_ptr);
  245. # else
  246.       free(struct_ptr);
  247. # endif
  248. #endif
  249. }
  250.  
  251.  
  252. /* Allocate memory.  For reasonable files, size should never exceed
  253.    64K.  However, zlib may allocate more then 64K if you don't tell
  254.    it not to.  See zconf.h and png.h for more information. zlib does
  255.    need to allocate exactly 64K, so whatever you call here must
  256.    have the ability to do that. */
  257.  
  258. #ifndef FORTIFY
  259. png_voidp
  260. png_malloc(png_structp png_ptr, png_uint_32 size)
  261. {
  262.    png_voidp ret;
  263.    if (!png_ptr || !size)
  264.       return ((voidp)0);
  265.  
  266. #ifdef PNG_MAX_MALLOC_64K
  267.    if (size > (png_uint_32)65536L)
  268.       png_error(png_ptr, "Cannot Allocate > 64K");
  269. #endif
  270.  
  271. #if defined(__TURBOC__) && !defined(__FLAT__)
  272.    ret = farmalloc(size);
  273. #else
  274. # if defined(_MSC_VER) && defined(MAXSEG_64K)
  275.    ret = halloc(size, 1);
  276. # else
  277.    ret = malloc(size);
  278. # endif
  279. #endif
  280.  
  281.    if (ret == NULL)
  282.    {
  283.       png_error(png_ptr, "Out of Memory");
  284.    }
  285.  
  286.    return ret;
  287. }
  288.  
  289. /* free a pointer allocated by png_malloc().  In the default
  290.   configuration, png_ptr is not used, but is passed in case it
  291.   is needed.  If ptr is NULL, return without taking any action. */
  292. void
  293. png_free(png_structp png_ptr, png_voidp ptr)
  294. {
  295.    if (!png_ptr)
  296.       return;
  297.  
  298.    if (ptr != NULL)
  299.    {
  300. #if defined(__TURBOC__) && !defined(__FLAT__)
  301.       farfree(ptr);
  302. #else
  303. # if defined(_MSC_VER) && defined(MAXSEG_64K)
  304.       hfree(ptr);
  305. # else
  306.       free(ptr);
  307. # endif
  308. #endif
  309.    }
  310. }
  311.  
  312. #endif /* FORTIFY */
  313. #endif /* Not Borland DOS special memory handler */
  314.  
  315.